# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name：     day160_pyqt5_dragDrop
   Description :
   Author :       fuml
   date：          2019/9/23
-------------------------------------------------
   Change Activity:
                   2019/9/23:This is a simple drag and
drop example.
自定义按钮控件,重写其相关方法实现可接受文本类型拖拽
QLineEdit 设置属性后即可实现接受文本类型拖拽
-------------------------------------------------
"""
import sys
from  PyQt5.QtWidgets import QWidget,QLineEdit,QPushButton,QApplication
from PyQt5.QtCore import Qt,QMimeData
class MyButton(QPushButton):
    def __init__(self,title,parent):
        super().__init__(title,parent)
        self.setAcceptDrops(True)

    def dragEnterEvent(self, e):
        if e.mimeData().hasFormat('text/plain'):
            e.accept()
        else:
            e.ignore()
    def dropEvent(self, e):
        self.setText(e.mimeData().text())

class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        edit = QLineEdit('',self)
        edit.setDragEnabled(True)
        edit.move(30,65)

        button = MyButton('Button',self)
        button.move(190,65)

        self.setWindowTitle('Simple Drag & Drop')
        self.setGeometry(300,300,300,150)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

